package org.bim.view.drag.design.util;

import lombok.extern.slf4j.Slf4j;
import org.bim.view.drag.design.constant.CommonConstant;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取用户Token的工具
 */
@Slf4j
public class TokenUtil {
    public static String getUserToken() {
        String token = "";
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

        String header = request.getHeader(CommonConstant.USER_TOKEN_KEY);

        token = StringUtils.isEmpty(header) ? request.getParameter(CommonConstant.TOKEN) : header;
        if ("null".equals(token) || StringUtils.isEmpty(token)) {
            token = null;
        }
        return token;
    }
}
